import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testcronjob:

    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/1_get_cronjob_list.yaml'))
    def test_get_cronjob_list(self, cronjobinfo, get_env_info):
        # 获取cronjob列表接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','ingress-nginx')
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/2_create_cronjob.yaml'))
    def test_create_cronjob(self, cronjobinfo, get_env_info):
        # 创建cronjob接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','default')
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in cronjobinfo['name']:
            cronjobname=req.json()['metadata']['name']
            globals()['cronjobname']=cronjobname
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/3_get_specific_cronjob.yaml'))
    def test_get_specific_cronjob(self, cronjobinfo, get_env_info):
        # 获取指定cronjob接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','default').replace('{cronjobname}',globals()['cronjobname'])
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        if '不正确' not in cronjobinfo['name']:
            updatereqbody=req.json()
            globals()['updatereqbody']=updatereqbody
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/4_update_cronjob.yaml'))
    def test_update_cronjob(self, cronjobinfo, get_env_info):
        # 更新指定cronjob接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','default').replace('{cronjobname}',globals()['cronjobname'])
        method = cronjobinfo['request']['method']
        data = globals()['updatereqbody']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/5_update_section_cronjob.yaml'))
    def test_update_section_cronjob(self, cronjobinfo, get_env_info):
        # 部分更新指定cronjob的接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','default').replace('{cronjobname}',globals()['cronjobname'])
        method = cronjobinfo['request']['method']
        data = cronjobinfo['request']['data']
        header = cronjobinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']


    @pytest.mark.parametrize('cronjobinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/cronjob/6_delete_specific_cronjob.yaml'))
    def test_delete_specific_cronjob(self, cronjobinfo, get_env_info):
        # 删除cronjob接口
        baseUrl = get_env_info
        url = baseUrl + cronjobinfo['request']['url'].replace('{namespace}','default').replace('{cronjobname}',globals()['cronjobname'])
        method = cronjobinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == cronjobinfo['validate'][0]['equals']['status_code']